﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Joyintech.Common;
using Joyintech.Model.inpiao365;
using Joyintech.BLL.inpiao365;
using Joyintech.Common.DEncrypt;
using System.Collections;
using System.Data;

namespace Joyintech.Admin.inpiao365
{
    public partial class login : System.Web.UI.Page
    {
        protected static string LoginTheme = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.txtUserName.Focus();

                systemInterface model = (systemInterface)systemUser_BLL.GetInstance().GetObject("Interface");

                //读取系统界面配置相关
                if (model == null)
                {
                    model = new systemInterface_BLL().GetModel(1);
                    systemUser_BLL.GetInstance().StoreObject("Interface", model);
                }

                this.Page.Title = model.MainIeTitle;

                LoginTheme = model.LoginTheme;

                if (DNTRequest.GetQueryString("logout") != "")
                {
                    Global.WriteLog("用户退出", "用户退出", systemUser_BLL.GetInstance().GetCurrentSystemUser().UserTrueName.ToString(), systemUser_BLL.GetInstance().GetCurrentSystemUser().UserName.ToString());
                    //清除Session
                    HttpContext.Current.Session.Clear();

                    Response.Redirect("Login.aspx");
                }
            }
        }


        /// <summary>
        /// 登录事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void inputLogin_ServerClick(object sender, EventArgs e)
        {
            #region 验证

            string strErr = "";

            if (this.txtUserName.Value == "")
            {
                strErr += "用户名　不可为空！\\n";
            }

            if (this.txtUserPwd.Value == "")
            {
                //strErr += "用户密码　不可为空！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this.Page, strErr);
                return;
            }

            //登录次数限制
            if ((HttpContext.Current.Session["PassErrorCount"] != null) && (HttpContext.Current.Session["PassErrorCount"].ToString() != ""))
            {
                int PassErroeCount = Convert.ToInt32(HttpContext.Current.Session["PassErrorCount"]);
                PassErroeCount += 1;
                HttpContext.Current.Session["PassErrorCount"] = PassErroeCount;

                if (PassErroeCount > 3)
                {
                    MessageBox.Show(this.Page, "对不起，你错误登录了三次，系统登录锁定！请二十分钟之后再试！");
                    return;
                }
            }
            else
            {
                HttpContext.Current.Session["PassErrorCount"] = 0;
            }


            #endregion

            #region 检查用户有效性

            systemUser model = new systemUser();

            systemUser_BLL bll = new systemUser_BLL();

            model = bll.GetModel(this.txtUserName.Value.ToString(), DEncrypt.Encrypt(this.txtUserPwd.Value.ToString()));

            if (model != null)
            {
                //重置登录错误记录
                HttpContext.Current.Session["ErrorLoginCount"] = null;

                //向Cookies中存储SessionId
                systemUser_BLL.GetInstance().StoreSessionId(HttpContext.Current.Session.SessionID);

                //向Memcached中存储SessionId对应的当前登录会员实体
                systemUser_BLL.GetInstance().StoreCurrentSystemUser(HttpContext.Current.Session.SessionID, model);

                //获取用户的角色权限
                if (model.UserRoles.ToString().Length > 0)
                {
                    List<systemRole> userRoleList = new List<systemRole>();

                    userRoleList = new systemRole_BLL().GetModelList("RoleId in (" + model.UserRoles + ")");

                    if (userRoleList != null && userRoleList.Count > 0)
                    {
                        string strLMID = string.Empty;

                        foreach (systemRole modelRole in userRoleList)
                        {
                            strLMID += modelRole.RoleFunCode + ",";
                        }

                        strLMID = strLMID.Replace(",,", ",");

                        ArrayList arrUserFun = new ArrayList();

                        string[] Masters = strLMID.Split(',');

                        foreach (string str in Masters)
                        {
                            if (!string.Empty.Equals(str))
                            {
                                arrUserFun.Add(str);
                            }
                        }

                        //HttpContext.Current.Session[Constants.SESSION_USERFUN] = arrUserFun;

                        //向Memcached中存储SessionId对应的当前登录会员权限
                        systemUser_BLL.GetInstance().StoreObject(HttpContext.Current.Session.SessionID + "_SESSION_USERFUN", arrUserFun);

                    }
                }

                //登录日志
                Global.WriteLog("登录成功", "用户系统", model.UserTrueName, model.UserName);

                Response.Redirect("Index.aspx");
            }
            else
            {
                MessageBox.Show(this.Page, "用户名或密码错误，请重新登录！");
            }

            #endregion
        }
    }
}